package zjh_20230327;

import java.lang.reflect.*;
import java.util.Arrays;

public class Demo01 {
    public static void main(String[] args) {
        Category category = new Category();
        Class<? extends Category> aClass = category.getClass();
        System.out.println(aClass);

        /**
         * 看类的访问修饰符
         */
        Class<Category> categoryClass1 = Category.class;
        int modifiers = categoryClass1.getModifiers();
        System.out.println(modifiers);
        System.out.println(Modifier.toString(modifiers));


        /**
         * constructor对象 指定的构造方法创建对象
         */
        Constructor<Category> constructor = null;
        Category category1 = null;
        try {
            constructor = Category.class.getConstructor();
//            category1 = constructor.newInstance();
            Category category3 = constructor.newInstance();
            Class<? extends Category> aClass1 = category3.getClass();
            Method say = aClass1.getMethod("say");
            System.out.println(say);


            //调用方法
            Category category2 = constructor.newInstance();
            Object invoke = say.invoke(category2);
            System.out.println("----------");
            System.out.println(say);


        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
        System.out.println(category1);


        Category category2 = new Category();
        Class<Category> categoryClass = Category.class;
        try {
           category2 = categoryClass.newInstance();
            Class<? extends Category> aClass1 = category2.getClass();
            Method[] methods = aClass1.getMethods();
            System.out.println(Arrays.toString(methods));


            Method say = aClass1.getMethod("say");
            System.out.println("==========");
            say.invoke(category2);

        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }  catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        System.out.println(category2);


    }
}
